package com.ssm.wechat.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ssm.pre.bean.Template;
import com.ssm.pre.util.TokenThread2;
import com.ssm.pre.util.WeixinUtil;
import com.ssm.util.Constants;
import com.ssm.wechat.dao.WechatLifeDao;
import com.ssm.wechat.service.UtilService;
import com.ssm.wechat.service.WechatLifeService;

import net.sf.json.JSONObject;

@Service
public class WechatLifeServiceImpl implements WechatLifeService {

	@Autowired
	private WechatLifeDao wechatLifeDao;

	/**
	 * 查询物业费账单（最新的一条记录）
	 */
	@Override
	public boolean sendPropertyMsg(Template tem) throws Exception {
		 boolean flag=false;
		 String result = null;
		 String url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+TokenThread2.accessToken.getToken();//请求接口的地址
		 Map<String, Object> params = new HashMap<String, Object>();//请求参数
//		 params.put("access_token", TokenThread2.accessToken.getToken());//调用接口凭证
		 params.put("access_token", WeixinUtil.getAccessToken(Constants.APP_ID, Constants.APPSECRET));//调用接口凭证
		 params.put("openid", tem.getToUser());
		 result = UtilService.net(url, params, "POST");
		 JSONObject object = JSONObject.fromObject(result);
		 if(object != null){
			 int errorCode = object.getInt("errcode");
			 String errorMessage = object.getString("errmsg");
			 if(errorCode==0){  
	                flag=true;  
	            }else{  
	                System.out.println("模板消息发送失败:"+errorCode+","+errorMessage);  
	                flag=false;  
	            }  
		 }
		return flag;
	}

	
	
}
